package com.deram.ctin.serv.enter.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import com.deram.ctin.common.bean.PageResult;
import com.deram.ctin.common.utils.BusinessUtils;
import com.deram.ctin.common.utils.DateUtils;
import com.deram.ctin.common.utils.ValidateUtils;
import com.deram.ctin.dependencies.serv.utils.MybatisUtils;
import com.deram.ctin.serv.enter.entity.EnterInfo;
import com.deram.ctin.serv.enter.filter.EnterInfoFilter;
import com.deram.ctin.serv.enter.mapper.EnterInfoMapper;
import com.deram.ctin.serv.enter.service.EnterInfoService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @Author: jtt
 * @Description:
 * @Date: 2017/9/12 10:28
 * @version: V1.0
 */
@Service
public class EnterInfoServiceImpl implements EnterInfoService {

    @Autowired
    EnterInfoMapper enterInfoMapper;

    @Override
    public EnterInfo find(Long id) {
        ValidateUtils.isId(id, "企业ID");
        EnterInfo enter = enterInfoMapper.selectById(id);
        BusinessUtils.notNull(enter, "企业信息不存在");
        return enter;
    }

    @Override
    public EnterInfo findByCode(String code) {
        ValidateUtils.notNull(code, "企业统一社会信用代码");
        EnterInfo filter = new EnterInfo();
        filter.setEnterCode(code);
        EnterInfo enter = enterInfoMapper.selectOne(filter);
        BusinessUtils.notNull(enter, "企业信息不存在");
        return enter;
    }

    @Override
    public EnterInfo findByName(String name) {
        ValidateUtils.notNull(name, "企业名称");
        EnterInfo filter = new EnterInfo();
        filter.setName(name);
        EnterInfo enter = enterInfoMapper.selectOne(filter);
        BusinessUtils.notNull(enter, "企业信息不存在");
        return enter;
    }

    @Override
    public List<EnterInfo> list(EnterInfoFilter filter) {

        EntityWrapper<EnterInfo> ew = createEntityWrapper(filter);
        List<EnterInfo> result = enterInfoMapper.selectList(ew);
        return result;
    }

    @Override
    public List<EnterInfo> searchByKey(String keywords) {
//        PageHelper.startPage(pagination.getCurrent(), pagination.getSize());
        Page<EnterInfo> result = new Page<>();
        EnterInfo filter = new EnterInfo();
        filter.setKeywords(keywords);
        List<EnterInfo> list = enterInfoMapper.searchByKey(filter);
        result.setRecords(list);
        return list;
    }

    @Override
    public List<EnterInfo> searchByFather(Long fatherId) {
        EnterInfo filter = new EnterInfo();
        filter.setFatherId(fatherId);
        List<EnterInfo> list = enterInfoMapper.searchByFather(filter);
        return list;
    }

    @Override
    public PageResult<EnterInfo> findByFather(EnterInfoFilter filter, Integer pageNo, Integer pageSize) {
        Pagination pagination = MybatisUtils.createPagination(pageNo, pageSize);
        EntityWrapper<EnterInfo> wrapper = createEntityWrapper(filter);
        return MybatisUtils.createPageResult(enterInfoMapper.selectPage(pagination,wrapper),pagination);
    }

    private EntityWrapper<EnterInfo> createEntityWrapper(EnterInfoFilter filter) {
        EnterInfo entity = new EnterInfo();
        entity.setFatherId(filter.getFatherId());
        EntityWrapper<EnterInfo> ew = MybatisUtils.createEntityWrapper(entity, filter);
        if(CollectionUtils.isNotEmpty(filter.getIdList())){
            ew.in("id", filter.getIdList());
        }
        return ew;
    }

    @Override
    public List<EnterInfo> findAll() {
        EnterInfo enterInfo = new EnterInfo();
        EntityWrapper<EnterInfo> enterInfoEntityWrapper = new EntityWrapper<>(enterInfo);
        enterInfoEntityWrapper.le("id",10);
        return enterInfoMapper.selectList(enterInfoEntityWrapper);
    }

    @Override
    public EnterInfo save(EnterInfo param) {
        if (null != param.getId()) {
            EnterInfo enter = find(param.getId());
            if (null == param.getEnterCode()) {
                param.setEnterCode(enter.getEnterCode());
            }
            enterInfoMapper.updateById(param);
        } else {
            enterInfoMapper.insert(param);
        }
        return param;
    }

    @Override
    public void insertList(List<EnterInfo> param) {
        if (null != param && !param.isEmpty()) {
            //enterInfoMapper.insertBatch(param);
            enterInfoMapper.batchInsert(param);
        }
    }

    @Override
    public EnterInfo saveByCode(EnterInfo param) {
        param.setUpdateDate(DateUtils.getNow());
        if (null != param.getEnterCode()) {
            EnterInfo enter = findByCode(param.getEnterCode());
            param.setId(enter.getId());
            enterInfoMapper.updateById(param);
        } else {
            enterInfoMapper.insert(param);
        }
        return param;
    }

    @Override
    public void delete(Long id) {
        enterInfoMapper.deleteById(find(id));
    }
}
